package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class UserManagePage extends Utils {
    public static String url = "http://dongliyang.online:10030/oj/layout/cuser";

    public UserManagePage() throws IOException, InterruptedException {
        super(url);
    }

    // 未登录状态 - 验证是否重定向到登录页面
    public void pageRightWithoutLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 导航到用户管理页面
        driver.get(url);

        // 等待重定向到登录页面
        wait.until(ExpectedConditions.urlContains("/oj/login"));
        assert driver.getCurrentUrl().contains("/oj/login") : "未登录状态下未重定向到登录页面";
        System.out.println("未登录状态下访问用户管理页面，重定向到登录页面，当前 URL: " + driver.getCurrentUrl());
        getScreenShot("userManagePageWithoutLogin");
    }

    // 已登录状态 - 验证页面加载
    public void pageRightWithLogin() throws IOException {
        // 导航到用户管理页面
        driver.get(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 等待页面加载完成（检查表格是否存在）
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[1]/div")
        ));

        System.out.println("已登录状态下用户管理页面加载成功。");
        getScreenShot("userManagePageWithLogin");
    }

    // 搜索功能 - 根据用户 ID 搜索并验证结果
    public void searchUserById(String userId) throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 定位搜索框并输入用户 ID
        WebElement searchInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[1]/div/div/div/input")
        ));
        searchInput.clear();
        searchInput.sendKeys(userId);

        // 定位并点击搜索按钮
        WebElement searchButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[3]/div/button[1]")
        ));
        searchButton.click();

        // 等待搜索结果加载（检查表格第一行第一列是否包含用户 ID）
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]/div")
        ));
        WebElement firstRowUserId = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]/div"));
        String actualUserId = firstRowUserId.getText().trim();
        assert actualUserId.equals(userId) : "搜索结果不正确，预期用户 ID: " + userId + ", 实际: " + actualUserId;

        System.out.println("成功搜索用户 ID: " + userId);
        getScreenShot("searchUser_" + userId);
    }

    // 拉黑功能 - 拉黑或取消拉黑用户，并验证状态
    public void toggleBlacklist(String userId, boolean blacklist) throws IOException, InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 搜索用户以确保目标用户在表格中
        searchUserById(userId);

        WebElement goback = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[3]/div/button[2]")
        ));
        goback.click();


        // 定位目标用户的操作列（最后一列）
        WebElement operationCell = wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[10]/div")
        ));

//        /html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[10]/div/button
        // 定位“拉黑”或“取消”按钮
        WebElement actionButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[10]/div/button")
        ));
        actionButton.click();


        // 等待状态更新（检查用户状态列）
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[9]/div/span/span")
        ));
        WebElement statusCell = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[9]/div/span/span"));
        String actualStatus = statusCell.getText().trim();
        String expectedStatus = blacklist ? "异常" : "正常";
        assert actualStatus.equals(expectedStatus) : "用户状态未正确更新，预期: " + expectedStatus + ", 实际: " + actualStatus;

        System.out.println("用户 ID: " + userId + " 已" + (blacklist ? "拉黑" : "取消拉黑") + "，状态: " + actualStatus);
        getScreenShot("blacklistUser_" + userId + "_" + (blacklist ? "blacklisted" : "unblacklisted"));
    }

    // 测试方法 - 综合测试
    public void testUserManagePage() throws IOException, InterruptedException {
        // 未登录状态测试
//        pageRightWithoutLogin();

        // 模拟登录（假设已有一个登录方法）
        // login("username", "password"); // 需要实现登录方法

        // 已登录状态测试
        pageRightWithLogin();

        // 搜索用户
        String userId = "1907122101606273025"; // 从截图中获取一个用户 ID
        searchUserById(userId);

        // 拉黑用户
        toggleBlacklist(userId, true);

        // 取消拉黑用户
        toggleBlacklist(userId, false);
    }
}